package com.tju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.eduservice.entity.EduCourse;
import com.tju.eduservice.entity.EduCourseDescription;
import com.tju.eduservice.entity.frontVo.CourseFrontVo;
import com.tju.eduservice.entity.frontVo.CourseWebVo;
import com.tju.eduservice.entity.vo.CourseInfoVo;
import com.tju.eduservice.entity.vo.CoursePublishVO;
import com.tju.eduservice.entity.vo.CourseQuery;
import com.tju.eduservice.mapper.EduCourseMapper;
import com.tju.eduservice.service.EduChapterService;
import com.tju.eduservice.service.EduCourseDescriptionService;
import com.tju.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.eduservice.service.EduVideoService;
import com.tju.serviceHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;




    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表格和 课程描述表格  分别添加信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int num = baseMapper.insert(eduCourse);//课程信息注入表格

        if(num == 0){
            throw new GuliException(20001,"添加课程信息失败");
        }

        String id = eduCourse.getId();

        //课程简介表中插入数据
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(id);
        courseDescriptionService.save(description);

        return id;

    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduCourseDescription description = courseDescriptionService.getById(courseId);

        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;

    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int col = baseMapper.updateById(eduCourse);
        if(col == 0){
            throw  new GuliException(20001,"修改课程信息失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }

    @Override
    public CoursePublishVO publishCourseInfo(String id) {
        CoursePublishVO course = baseMapper.getPublishCourse(id);
        return course;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        //是否传入状态
        if(! StringUtils.isEmpty(status)){
            wrapper.eq("status",status);

        }
        //排序
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam,wrapper);
    }

    @Override
    public boolean removeCourse(String id) {
        //1、根据课程id删除小节
        videoService.removeVideoByCourseId(id);

        //2、根据课程id删除章节部分
        chapterService.removeChapterByCourseId(id);

        //3、根据课程id删除课程描述
        courseDescriptionService.removeById(id);

        //4、根据课程id删除课程本身
        int result = baseMapper.deleteById(id);

        if (result>=1){
            return true;
        }else {
            throw new GuliException(20001,"删除失败");
        }

    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格
            wrapper.orderByDesc("price");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//最新，创建时间
            wrapper.orderByDesc("gmt_create");
        }
        baseMapper.selectPage(pageCourse,wrapper);

        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> courseList = pageCourse.getRecords();//数据集合
        long size = pageCourse.getSize();//每页记录数
        long current = pageCourse.getCurrent();//当前页
        long pages = pageCourse.getPages();//总页数
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页
        boolean hasNext = pageCourse.hasNext();//是否有下一页

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",courseList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
